package personal.xjl._23java1studentdemo.mapper;

import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Controller;
import personal.xjl._23java1studentdemo.model.Course;
import personal.xjl._23java1studentdemo.model.Score;
import personal.xjl._23java1studentdemo.model.Student;
import personal.xjl._23java1studentdemo.model.Teacher;

import java.util.List;

@Mapper
public interface CourseMapper {

    @Select("select * from course")
    List<Course> selectAll();

    @Delete("delete from course where id =#{id}")
    int delete(int id);

    @Insert("insert into course( id,name,teacher_id ) values (#{id},#{name},#{teacher_id})")
    int insert(Course course);


    @Select("select * from course where id=#{id}")
    Course findById(int id);

    @Update("update course set name=#{name},teacher_id=#{teacher_id} where id =#{id}")
    int update(Course course);

    @Select("select * from course limit #{fromIndex},#{pageSize}")
    @Results({
            @Result(column = "id",property = "id",javaType = Integer.class),
            @Result(column = "teacher_id", property = "teacher", javaType = Teacher.class,
                    one = @One(select = "personal.xjl._23java1studentdemo.mapper.TeacherMapper.selectById")),
            @Result(column = "id", property = "scores", javaType = List.class,
                    many = @Many(select = "personal.xjl._23java1studentdemo.mapper.ScoreMapper.selectByCourseId")
            )
    })
    List<Course> findByPage(int fromIndex, int pageSize);
}
